"""
Case Type   : Compatibility
Case Name   : 验证dolphin.b_compatibility_mode参数设置方式
Create At   : 2022/08/15
Owner       :@zou_jialiang0501162243
Description :
        1.gs_guc set方式设置
        2.gs_guc reload方式设置
        3.ALTER SYSTEM SET方式设置
        4.修改指定为数据库级别
        5.修改指定为用户级别
        6.修改指定为会话级别
Expect      :
        1.参数设置失败
        2.参数设置失败
        3.参数设置失败，合理报错
        4.修改成功，修改成功，当前session参数不生效，新session参数生效
        5.修改成功，修改成功，当前session参数不生效，新session参数生效
        6.修改成功，参数生效
History     :modified by zhanghuan96,2024.6.20，
            参数dolphin.b_compatibility_mode参数默认值变更影响stpe4结果
"""
import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import macro


class Operator(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.constant = Constant()
        self.commonsh = CommonSH('dbuser')
        self.db_name = 'db_operator0064'
        self.user = 'u_operator0064'
        self.pwd = 'test@123'
        text = '----pre_step:创建兼容B库和用户；expect:创建成功----'
        self.log.info(text)
        create_us1 = f"drop user if exists {self.user};" \
                     f"create user {self.user} password '{self.pwd}';"
        res1 = self.commonsh.execut_db_sql(sql=create_us1)
        self.log.info(res1)
        self.assertIn(self.constant.CREATE_ROLE_SUCCESS_MSG, res1,
                      '执行失败' + text)
        create_db = f"drop database if exists {self.db_name};" \
                    f"create database {self.db_name} dbcompatibility = 'B';" \
                    f"\c {self.db_name}"
        res = self.commonsh.execut_db_sql(create_db)
        self.log.info(res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, res,
                      '执行失败' + text)
        msg = f'You are now connected to database "{self.db_name}"'
        self.assertIn(msg, res, '执行失败' + text)

    def test_operator(self):
        text = '----step1:gs_guc set方式设置；expect:参数设置失败----'
        self.log.info(text)
        res1 = self.commonsh.execute_gsguc('set',
                                           self.constant.GSGUC_SUCCESS_MSG,
                                           'dolphin\.b_compatibility_mode = '
                                           'true')
        self.log.info(res1)
        self.assertFalse(res1, '执行失败' + text)

        text = '----step2:gs_guc reload方式设置；expect:参数设置失败----'
        self.log.info(text)
        res2 = self.commonsh.execute_gsguc('set',
                                           self.constant.GSGUC_SUCCESS_MSG,
                                           'dolphin\.b_compatibility_mode = '
                                           'true')
        self.log.info(res2)
        self.assertFalse(res2, '执行失败' + text)

        text = '----step3:ALTER SYSTEM SET方式设置；expect:参数设置失败，' \
               '合理报错----'
        self.log.info(text)
        res3 = self.commonsh.execut_db_sql(f'''alter system set \
        dolphin.b_compatibility_mode to 'true';''', dbname=self.db_name)
        self.log.info(res3)
        einfo1 = 'NOTICE:  USERSET level parameter change may cause guc config changes in other sessions'
        self.assertIn(einfo1, res3, '执行失败' + text)

        text = '----step4:修改指定为数据库级别；expect:修改成功，' \
               '当前session参数不生效，新session参数生效----'
        self.log.info(text)
        sql1 = f"alter database {self.db_name} set " \
               f"dolphin.b_compatibility_mode to 'true';" \
               f"select 1&&2;"
        sql2 = f"select 1&&2;alter database {self.db_name} " \
               f"reset dolphin.b_compatibility_mode;"
        res4 = self.commonsh.execut_db_sql(sql1, dbname=self.db_name)
        self.log.info(res4)
        self.assertIn(self.constant.ALTER_DATABASE_SUCCESS_MSG and 't',
                      res4, '执行失败' + text)
        res5 = self.commonsh.execut_db_sql(sql2, dbname=self.db_name)
        self.log.info(res5)
        self.assertIn(' t' and self.constant.ALTER_DATABASE_SUCCESS_MSG, res5,
                      '执行失败' + text)

        text = '----step5:修改指定为用户级别；expect:修改成功，' \
               '当前session参数不生效，新session参数生效（参数默认值变更为on)----'
        self.log.info(text)
        sql3 = f"alter user {self.user} set dolphin.b_compatibility_mode " \
               f"to 'true';select 1&&2;"
        res6 = self.commonsh.execut_db_sql(sql3, dbname=self.db_name)
        self.log.info(res6)
        self.assertIn(self.constant.ALTER_ROLE_SUCCESS_MSG and 't', res6,
                      '执行失败' + text)
        excute_cmd = f'''source {macro.DB_ENV_PATH};\
        gsql -d {self.db_name} -p {self.commonsh.node.db_port} -U {self.user} \
        -W {self.pwd} -c "select 1&&2;\
        alter user {self.user} reset dolphin.b_compatibility_mode;"'''
        self.log.info(excute_cmd)
        res7 = self.commonsh.node.sh(excute_cmd).result()
        self.log.info(res7)
        self.assertIn(' t' and self.constant.ALTER_ROLE_SUCCESS_MSG, res7,
                      '执行失败' + text)

        text = '----step6:修改指定为会话级别；expect:修改成功，参数生效----'
        self.log.info(text)
        res8 = self.commonsh.execut_db_sql(f"set dolphin.b_compatibility_mode "
                                           f"to 'true';select 1&&2;",
                                           dbname=self.db_name)
        self.log.info(res8)
        self.assertIn(' t', res8, '执行失败' + text)

    def tearDown(self):
        self.log.info('----清理环境----')
        text = '----删除用户和兼容B库；expect:删除成功----'
        self.log.info(text)
        drop_cmd = f"drop user if exists {self.user};"
        res = self.commonsh.execut_db_sql(drop_cmd)
        self.log.info(res)
        res0 = self.commonsh.execut_db_sql(f"drop database if exists "
                                             f"{self.db_name};")
        self.log.info(res0)
        self.assertEqual(self.constant.DROP_ROLE_SUCCESS_MSG, res,
                         '执行失败' + text)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, res0,
                      '执行失败' + text)
        self.log.info(f'----{os.path.basename(__file__)} end----')
